clear all
set more off
set maxvar 10000


    file open topbottom using "$Mydirectory2/appendix_e/tracking_topbottom_coded.tex", write replace
        foreach s in anes avtmh57 avtmh76 gss nfs nlsmw nlsom nlsy79 nlsym66 nlsyw68 nsba nsfh ocg62 ocg73 psid1997 psid2017  {

    *-------------------------------------------*
    *  STEP 1: DEFINE LOCALS + FIX PSID  
    *-------------------------------------------*
        if "`s'"=="anes" {
            local name "American National Election Survey, 1956--1970"
            local f "ANES"
            local subf2 "output"
            local dta2 "ANES56to70_analysis.dta"
        }            
        if "`s'"=="avtmh57" {
            local name "Americans View Their Mental Health, 1957"
            local f "Americans_MentalHealth_1957"
            local subf2 "output"
            local dta2 "AVTMH1957_IGEanalysis.dta"
        }
        if "`s'"=="avtmh76" {
            local name "Americans View Their Mental Health, 1976"
            local f "Americans_MentalHealth_1976"  
            local subf2 "output"
            local dta2 "AVTMH1976_cleaned.dta"
        }
        if "`s'"=="gss" {
            local name "General Social Surveys, 1977--2018"
            local f "GSS"
            local subf2 "output"
            local dta2 "GSS_allyears_foranalysis.dta"
        }
        if "`s'"=="nfs" {
            local name "National Fertility Study, 1970"
            local f "FertilityStudy_1970"
            local subf2 "output"
            local dta2 "NFS70_analysis.dta"
        }
        if "`s'"=="nlsmw" {
            local name "National Longitudinal Survey of Mature Women, 1967"
            local f "NLS_MatureWomen"
            local subf2 "output"
            local dta2 "NLSMW_analysis.dta"
        }
        if "`s'"=="nlsom" {
            local name "National Longitudinal Survey of Older Men, 1966"
            local f "NLS_older_men"
            local subf2 "output"
            local dta2 "nls_older_cleaned"
        }
        if "`s'"=="nlsy79" {
            local name "National Longitudinal Survey of Youth, 2002"
            local f "NLSY79"
            local subf2 "wrkdata"
            local dta2 "nlsy79_newxsec.dta"
        }
        if "`s'"=="nlsym66" {
            local name "National Longitudinal Survey of Young Men, 1981"
            local f "NLSYM66"
            local subf2 "wrkdata"
            local dta2 "nlsym66_newxsec.dta"
        }
        if "`s'"=="nlsyw68" {
            local name "National Longitudinal Survey of Young Women, 1988"
            local f "NLSYW68"
            local subf2 "wrkdata"
            local dta2 "nlsyw68_newxsec.dta"
        }
        if "`s'"=="nsba" {
            local name "National Survey of Black Americans, 1979--1980"
            local f "NSBA"
            local subf2 "output"
            local dta2 "cleaned_nsba.dta"
        }
        if "`s'"=="nsfh" {
            local name "National Survey of Families \& Households, 1987--1988"
            local f "natl_survey_fam_HH"
            local subf2 "output"
            local dta2 "fam_hh_cleaned.dta"
        }
        if "`s'"=="ocg62" {
            local name "Occupational Changes in a Generation, 1962"
            local f "CPS_1962_1973"
            local subf2 "output"
            local dta2 "OCG62_IGEanalysis.dta"
        }
        if "`s'"=="ocg73" {
            local name "Occupational Changes in a Generation, 1973"
            local f "CPS_1962_1973"
            local subf2 "output"
            local dta2 "OCG73_IGEanalysis.dta"
        }
        if "`s'"=="psid1997" {
            local name "Panel Study of Income Dynamics, 1997"
            local f "PSID97_CrossSection"  
            local subf2 "output"
            local dta2 "psid1997_cleaned.dta"
        }
        if "`s'"=="psid2017" {
            local name "Panel Study of Income Dynamics, 2017"
            local f "PSID17_CrossSection" 
            local subf2 "output"
            local dta2 "psid2017_cleaned.dta"
        }
       
    *-------------------------------------------------------*
    /*  COLUMNS 1 & 2: SHARE BOTTOMCODED (1) OR 
                       TOPCODED (2)  PER SURVEY  */
    *-------------------------------------------------------*
	
		use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
        keep if baseline_sample==1

        clonevar data2 = data 
        replace data2 = "psid1997" if year==1997 & data=="psid"
        replace data2 = "psid2017" if year==2017 & data=="psid"

            foreach var of varlist bottomcoded_son topcoded_son {
                summ `var' if data2=="`s'" 
                    
                    if "`var'"=="bottomcoded_son" {
                        local bottom = `r(mean)'
                        local bottom2: display %-09.2fc `bottom' 
                    }
                    if "`var'"=="topcoded_son" {
                        local top = `r(mean)'
                        local top2: display %-09.2fc `top'
                    }
             }

    *-------------------------------------------------------*
    /*  COLUMN 3: GRAB # BINS FOR FAMILY INCOME 
                  IN EACH SURVEY  */
    *-------------------------------------------------------*

        * Bring in cleaned data
            use "$Mydirectory1/1_DataSources/`f'/`subf2'/`dta2'" , clear

                if "`s'"!="anes" & "`s'"!="gss" {
                    codebook fam_inc_real
                    bysort fam_inc_real: gen tag = _n==1 if fam_inc_real!=.
                    egen total_bins = sum(tag) 

                    summ total_bins
                    local numbins = `r(mean)'
                    di "`numbins'"
                    local numbins2: display %-09.0fc `numbins'
                }
                else  {
                    bysort year fam_inc_real: gen tag = _n==1 if fam_inc_real!=.
                    bysort year: egen total_bins = sum(tag)

                    summ total_bins, d
                    local numbins = `r(p50)' //grab median
                    di "`numbins'"
                    local numbins2: display %-09.0fc `numbins'
                }

          file write topbottom "`name' & `bottom2' & `top2' & `numbins2' \\" _n

        }

    file close topbottom 